﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data.SqlClient;
namespace UcAsp.Net.ModelCreate
{
   public  class Models
   {
       
       private string _entitestxt = string.Empty;
       private string _tabletxt = string.Empty;
       private string _copyright = @"
#region 创建UcAsp.Net ModelsCreate 
//copyright @2011 UcAsp.NeT
//作者：ServuCn@hotmail.com 
#endregion\r\n";
       private static string Path
       {
           get { return AppDomain.CurrentDomain.BaseDirectory ; }
       }
       public string entitiestxt
       {
           get {
               if (this._entitestxt == string.Empty)               
                        _entitestxt= _copyright+  File.ReadAllText(Path + "\\Entity.temp"); 
               return this._entitestxt; 
           }
       }
       public string tabletxt
       { 
        get{
            if (_tabletxt == string.Empty)
                _tabletxt =_copyright+ File.ReadAllText(Path + "\\Table.temp");
            return _tabletxt;
            
        }

       }
       private  List<Tables> Create(string server, string login, string passwords, string databasename)
       {
           List<Tables> table = new List<Tables>();
           DataBase database = new DataBase("Data Source=" + server + ";Initial Catalog=" + databasename + ";User ID=" + login + ";Password=" + passwords + ";MultipleActiveResultSets=True");
           SqlDataReader rd = database.ExecuteReader(TableSql.TablesSql);
           while (rd.Read())
           {
               Tables t = new Tables();
               t.TableName = rd.GetString(0);
               t.Content = string.Empty;
               table.Add(t);

           }
           return table;
       }
       private Tables TableModels(string server, string login, string passwords, string databasename, Tables table, ref Tables temptable)
       {

           temptable.TableName = table.TableName;
           string content = string.Empty;
           DataBase database = new DataBase("Data Source=" + server + ";Initial Catalog=" + databasename + ";User ID=" + login + ";Password=" + passwords + ";MultipleActiveResultSets=True");
           SqlDataReader rd = database.ExecuteReader(TableSql.AllTableInfo2005 + "'" + table.TableName + "'");
           StringBuilder app = new StringBuilder();
           while (rd.Read())
           {
               if (rd.GetString(2) == "√")
               {
                  app.Append("[Key]\r\n");
               }
               if (rd.GetString(4) == "False" && rd.GetString(2) == "")
               {
                   app.Append("         [Required(ErrorMessage = \"" + rd.GetString(5) + "不能空\")]\r\n");
               }
               if (rd.GetString(5) != string.Empty)
               {
                   app.Append("         [DisplayName(\"" + rd.GetString(5) + "\")]\r\n ");
               }
               List<Property> list = Sql2Csharp.List();
               string s = rd.GetString(3);
               Property cp = list.Find(c => c.SqlType == rd.GetString(3));

               app.Append("         public " + cp.CsharpType + " " + rd.GetString(1) + " { get; set; }\r\n");
               

           }
           temptable.Content = app.ToString();
           return temptable; 
       }
       private Tables TableForkeys(string server, string login, string passwords, string databasename, Tables table, ref Tables temptable)
       {

           string content = string.Empty;
           DataBase database = new DataBase("Data Source=" + server + ";Initial Catalog=" + databasename + ";User ID=" + login + ";Password=" + passwords + ";MultipleActiveResultSets=True");
           SqlDataReader rd = database.ExecuteReader(TableSql.ForkeyFtable + "'" + table.TableName + "'");
           StringBuilder app = new StringBuilder();
           while (rd.Read())
           {


               app.Append("         [ForeignKey(" + rd.GetString(1) + ")]\r\n");
               app.Append("         public virtual " + rd.GetString(0) + " " + rd.GetString(0) + " { get; set; }\r\n");
               

           }

           temptable.Content = temptable.Content + app.ToString();
           return temptable;
       }
       private Tables TableIndex(string server, string login, string passwords, string databasename, Tables table, ref Tables temptable)
       {

           string content = string.Empty;
           DataBase database = new DataBase("Data Source=" + server + ";Initial Catalog=" + databasename + ";User ID=" + login + ";Password=" + passwords + ";MultipleActiveResultSets=True");
           SqlDataReader rd = database.ExecuteReader(TableSql.ForkeyIndextable + "'" + table.TableName + "'");
           StringBuilder app = new StringBuilder();
           while (rd.Read())
           {

               app.Append("         [ForeignKey(" + rd.GetString(1) + ")]\r\n");
               app.Append("         public virtual ICollection<" + rd.GetString(2) + "> " + rd.GetString(2) + " { get; set; }\r\n");


           }
           temptable.Content = temptable.Content + app.ToString();
           return temptable;
       }
       public bool CreateModel(string server, string login, string passwords, string databasename,string spacename,string entities)
       {
           List<Tables> table = Create(server, login, passwords, databasename);
           
           int i=0;
           string tabletxt = File.ReadAllText(Path + "Table.temp");
           foreach (Tables t in table)
           {
               tabletxt = tabletxt.Replace("$classnames$", spacename);
               tabletxt = tabletxt.Replace("$tablename$",t.TableName);
               Tables _temptable=new Tables();
               TableModels(server, login, passwords, databasename, t,ref _temptable);
               TableForkeys(server, login, passwords, databasename, t, ref _temptable);
               TableIndex(server, login, passwords, databasename, t, ref _temptable);
               tabletxt = tabletxt.Replace("$content$", _temptable.Content);
               File.WriteAllText(Path + "\\Models\\" + _temptable.TableName + ".cs", _copyright+tabletxt);
           }

           return true;
       }
        

    }
   public class Tables
   {
       public string TableName { get; set; }
       public string Content { get; set; }
   }
}
